<?php

/**
 * Mongodb操作类
 */
class DbMongo {

    var $mongo;
    var $db;
    var $error;

    /**
     * 初始化创建mongo连接
     * param $mongo_server:连接字符串
     * param $connect:true or false,如果为false则驱动会在必要的时候进行连接，或者可以手动调用connect进行连接
     */
    function __construct($mongo_server, $connect = true) {
        try {
            //MongoClient
            $this->mongo = new Mongo($mongo_server, array(
                'connect' => $connect
            ));
        } catch (MongoConnectionException $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 如果初始化connect参数为false，则可以手动创建连接
     */
    function connect() {
        try {
            $this->mongo->connect();
            return true;
        } catch (MongoConnectionException $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 选择数据库
     * param $dbname:数据库名称
     */
    function selectDb($dbname) {
        $this->db = $dbname;
    }

    /**
     * 插入文件
     * param $table_name:文件名称
     * param $record:插入数据
     */
    function insert($table_name, $record) {
        $dbname = $this->db;
        try {
            $this->mongo->$dbname->$table_name->insert($record);
            return true;
        } catch (MongoException $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 查找文档返回一条信息
     * param $condition:查询条件
     * param $fields:返回字段
     */
    function findOne($table_name, $condition, $fields = array()) {
        $dbname = $this->db;
        return $this->mongo->$dbname->$table_name->findOne($condition, $fields);
    }

    /**
     * 去掉重复
     * param $field:重复字段
     */
    function distInct($table_name, $condition, $field) {
        $dbname = $this->db;
        return $this->mongo->$dbname->$table_name->distinct($field, $condition);
    }

    /**
     * 获取文件中文档的总数
     */
    function count($table_name) {
        $dbname = $this->db;
        return $this->mongo->$dbname->$table_name->count();
    }

    /**
     * 根据条件获取文件中文档的总数
     */
    function countCondition($table_name, $condition) {
        $dbname = $this->db;
        return $this->mongo->$dbname->$table_name->find($condition)->count();
    }

    /**
     * 查找多条文档
     * param $table_name:文件名
     * param $query_condition:查询条件
     * param $result_conditon:结果过滤
     * param $fields:返回字段
     */
    function find($table_name, $query_condition, $result_condition = array(), $fields = array()) {
        $dbname = $this->db;
        $cursor = $this->mongo->$dbname->$table_name->find($query_condition, $fields);
        if (!empty($result_condition ['start'])) {
            $cursor->skip($result_condition ['start']);
        }
        if (!empty($result_condition ['limit'])) {
            $cursor->limit($result_condition ['limit']);
        }
        if (!empty($result_condition ['sort'])) {
            $cursor->sort($result_condition ['sort']);
        }
        $result = array();
        try {
            while ($cursor->hasNext()) {
                $result [] = $cursor->getNext();
            }
        } catch (MongoConnectionException $e) {
            $this->error = $e->getMessage();
            return false;
        } catch (MongoCursorTimeoutException $e) {
            $this->error = $e->getMessage();
            return false;
        }
        return $result;
    }

    /**
     * 创建索引
     * param $table_name:文件名
     * param $index:索引
     * param $index_param:索引参数
     * 复合索引：ensureIndex($db_name,{"id":1,"age":-1});
     * 1 or -1:id索引以升序存储，age索引以降序存储
     */
    function ensureIndex($table_name, $index, $index_param = array()) {
        $dbname = $this->db;
        try {
            $this->mongo->$dbname->$table_name->ensureIndex($index, $index_param);
            return true;
        } catch (MongoCursorException $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 删除索引
     * param $table_name:文件名
     * param $index:索引
     */
    function dropIndex($table_name, $index) {
        $dbname = $this->db;
        try {
            $this->mongo->$dbname->$table_name->dropIndex($index);
        } catch (MongoCursorException $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 删除文档
     * param $table_name:文件名
     * param $condition:删除条件
     * param $options:删除选项
     */
    function remove($table_name, $condition, $options = array()) {
        $dbname = $this->db;
        try {
            $this->mongo->$dbname->$table_name->remove($condition, $options);
            return true;
        } catch (MongoCursorException $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 修改文档
     * param $table_name:文件名
     * param $condition:修改条件
     * param $newdata:对象与更新匹配的记录
     * param $options:修改选项
     */
    function update($table_name, $condition, $newdata, $options = array()) {
        $dbname = $this->db;
        try {
            $this->mongo->$dbname->$table_name->update($condition, $newdata, $options);
            return true;
        } catch (MongoCursorException $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 获取错误信息
     */
    function getError() {
        return $this->error;
    }

}
